

************************************************************************************************************************
/**** This do-file runs regressions on RR policy on the multiple bank relationship sample and calls different DTA files as needed.

IMPORTANT: Run do-file 02_RRs_comprehensive before running this code. Othersive the dta for this code WILL NOT be ready! 
***********************************************************************************************************************/

************************************************************************************************************************
/* In details, this do-file builds Tables 1 (Summary),2,4,5,6,7,8, 12B and 13B. We focus on multiple bank relationship firms for superior control over credit demand shifts, with firm fixed effects. We call the single bank relationship firms file to build the last columns of Tables 4 and 5.     */
************************************************************************************************************************ 

*********************************
* Loading ADOs and Pseudo DTA
********************************
clear all
clear matrix
clear mata
clear results
set more off, permanently

* There is nothing special in my ado folder. We consider reghdfe, outreg2, center and unique (as well as its dependencies) enough to run this code

adopath + "//sbcdf060/Depep01$/ado"
adopath ++ "//sbcdf060/Depep01$/ado"
global dta_file = "D:\Countercyclical\Dta_files"
global output_file = "D:\Countercyclical\Output_files"


* This file contains Pseudo Data for the two RR episodes (easing and tightening)
use "$dta_file/PseudoData_RRmbr.dta", clear



/*******************************
/* Creating Table 1 (Summary) */
*******************************/

* The main dependent variable, changes in credit exposure. This is simply the quarterly change of total credit for each firm-bank pair. Policy interventions are within the window. So, this reflects the change in "after" minus "before" RR policy
label var dln3_amount_bank "Dln_loan"

* The Treatment variable is the counterfactual for the easing policy of RRs
label var RReasing "ResReq"

* The Treatment variable is the counterfactual for the tightening policy of RRs
label var RRtightening "ResReq"

* The additional dependent variable at the firm-level. Firm-level credit contains all credit provided by all financial institutions to firm "f". So, this reflects the change in "after" minus "before" the policy for each firm (and not firm-bank pair)
label var dln3_amount_firm "Dln_firm"

*Labeling controls
label var L3_total_interest "interest"

	
	** Panel A: Easing of RRs
cd $output_file
	  label var RReasing "ResReq"
	 estpost tabstat dln3_amount_bank L3_collateral L3_scline L3_foreign_currency L3_ln_r_maturity L3_total_interest RReasing if time=="easing", ///
		listwise statistics (min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table1A_LoanControls_RR_easing", cells("min p10 p50 mean p90 max sd count") mtitles("Loan Controls_RR_easing") nonumber csv replace label
		
		
	preserve
		       *Lagged (ln) of loan-level credit exposure 	
    	        gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen mshare2= abank/afirm2
				* Treatment variable (which is bank-level) weighted at firm-level 
				gen RR_firm = RReasing*mshare2
				
				
				
		     keep if time=="easing"
			 
			 collapse (sum) RR_firm (mean) dln3_amount_firm ln_L3_amount_firm L3_ln_employees , by(firm_id time )  
		label var dln3_amount_firm "Dln_firm"
		label var ln_L3_amount_firm "firm credit"
		label var L3_ln_employees "n employees"
		label var RR_firm "ResReq"
		
		estpost tabstat dln3_amount_firm ln_L3_amount_firm L3_ln_employees RR_firm, ///
		listwise statistics (min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table1A_FirmControls_RR_easing", cells("min p10 p50 mean p90 max sd count") mtitles("Firm Controls_RR_easing") nonumber csv replace label


restore 

preserve
    keep if time=="easing"
	bysort bank_id time: gen first=_n==1
	keep if first==1
			 
		estpost tabstat L3_ln_size L3_capital L3_liquidity L3_npl foreign commercial gov small  RReasing, ///
		listwise statistics (min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table1A_BankControls_RR_easing", cells("min p10 p50 mean p90 max sd count") mtitles("Bank Controls_RR_easing") nonumber csv replace label


restore 		
** The estimates of these three levels are appended and edited manually from the three csv files just generated by this piece of code, in excel. 


	** Panel B: Tightening of RRs
	 label var RRtightening "ResReq"
	 estpost tabstat dln3_amount_bank L3_collateral L3_scline L3_foreign_currency L3_ln_r_maturity L3_total_interest RRtightening if time=="tightening", ///
	listwise statistics(min p10 p50 mean p90 max sd count) columns(statistics) esample 
	esttab using "Table1B_LoanControls_RR_tightening", cells("min p10 p50 mean p90 max sd count") mtitles("Loan ControlsRR_tightening") nonumber csv  replace label
		
		
	preserve
		       *Lagged (ln) of loan-level, credit exposure 	
    	        gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen mshare2= abank/afirm2
				*Weighting bank-level var into firm-level
				gen RR_firm = RRtightening*mshare2
				* Firm-level treatment variable
				
							
		         keep if time=="tightening"
			 
			 collapse (sum)  RR_firm (mean) dln3_amount_firm ln_L3_amount_firm L3_ln_employees , by(firm_id time )  
			 
		label var dln3_amount_firm "Dln_firm"
		label var ln_L3_amount_firm "firm credit"
		label var L3_ln_employees "n employees"
		label var RR_firm "ResReq"
		
			estpost tabstat dln3_amount_firm ln_L3_amount_firm L3_ln_employees RR_firm, ///
			listwise statistics(min p10 p50 mean p90 max sd count) columns(statistics) esample
			esttab using "Table1B_FirmControls_RR_tightening", cells("min p10 p50 mean p90 max sd count") mtitles("Firm Controls_RR_tightening") nonumber csv replace label


restore 

preserve
    keep if time=="tightening"
	bysort bank_id time: gen first=_n==1
	keep if first==1
			 
			estpost tabstat  L3_ln_size L3_capital L3_liquidity L3_npl foreign commercial gov small  RRtightening, ///
		listwise statistics(min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table1B_BankControls_RR_tightening", cells("min p10 p50 mean p90 max sd count") mtitles("Bank Controls_RR_tightening") nonumber csv replace label


restore 		
*** End of Table 1.The estimates of these three levels are manually appended and edited in excel from the six csv files just generated by this piece of code . 



* Defining loan-level dependent (winsorized)
local dep_var wd3ln_amount_bank


* Defining controls (winsorized and the same used across this whole paper)

global bank_controls   wL3_capital wL3_liquidity wL3_ln_size  gov foreign small commercial wL3_npl  

global risk_controls   L3_collateral wL3_scline L3_foreign_currency wL3_ln_r_maturity wL3_total_interest

global firm_controls    wln_L3_amount_firm wL3_ln_employees   

global clusters         bank firm_industry2


/**********************
   * Creating Table 2 *
**********************/ 
drop n_firms n_sectors
gen n_firms =0 
gen n_sectors =0 
gen n_banks = 0 



	
local dep_var $dep_var 
local time_var  easing tightening 
local treat_var $treat_var 


foreach d of local dep_var {
foreach t of local time_var {

		
* Counting the number of firms in each regression
unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

unique firm_industry2 if !mi(`t')
replace n_sectors = r(sum) if !mi(`t')

sum n_sectors if !mi(`t')
local n_sectors = r(mean)

unique bank_id if !mi(`t')
replace n_banks = r(sum) if !mi(`t')

sum n_banks if !mi(`t')
local n_banks = r(mean)

gen RR = cond("`t'" == "easing", wRReasing, wRRtightening) 
label var RR "ResReq"


* Column 1 and 6
reghdfe `d' RR  if !mi(`t'), absorb(cons) vce(cluster $clusters)
outreg2 using Table2, excel append label ctitle(`t')  dec(3) addtext(Loan Controls, No, Firm Controls, No, Firm FE, No, N firms, `n_firms', N sectors, `n_sectors', N banks, `n_banks') nocons 

* Column 2 and 7
reghdfe `d' RR $risk_controls $firm_controls if !mi(`t'), absorb(sec2_mun) vce(cluster $clusters)
outreg2 using Table2, excel append label ctitle(`t')  dec(3) addtext(Loan Controls, Yes, Firm Controls, Yes, Firm FE, No, N firms, `n_firms', N sectors, `n_sectors', N banks, `n_banks') nocons keep(RR)

* Column 3 and 8
reghdfe `d' RR $risk_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table2, excel append label ctitle(`t')  dec(3) addtext(Loan Controls, Yes, Firm Controls, "-", Firm FE, Yes, N firms, `n_firms', N sectors, `n_sectors', N banks, `n_banks') nocons keep(RR)

* Column 4 and 9
reghdfe `d' RR $risk_controls $firm_controls $bank_controls if !mi(`t'), absorb(sec2_mun) vce(cluster $clusters)
outreg2 using Table2, excel append label ctitle(`t')  dec(3) addtext(Loan Controls, Yes, Firm Controls, Yes, Firm FE, No, N firms, `n_firms', N sectors, `n_sectors', N banks, `n_banks') nocons keep(RR $bank_controls)

* Column 5 and 10
reghdfe `d' RR $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table2, excel append label ctitle(`t')  dec(3) addtext(Loan Controls, Yes, Firm Controls, "-", Firm FE, Yes, N firms, `n_firms', N sectors, `n_sectors', N banks, `n_banks') nocons keep(RR $bank_controls)

drop RR
}
}
drop n_firms n_sectors n_banks


 * Centering var capital
bysort time: center wL3_capital	
gen capital = c_wL3_capital
label var capital "capital"

global bank_controls capital wL3_liquidity wL3_ln_size  gov foreign small commercial wL3_npl  

/**********************
  * Creating Table 4*
 **********************/ 

local dep_var $dep_var
local time_var easing 


foreach d of local dep_var {
foreach t of local time_var {


gen ResReq = wRReasing
label var ResReq "ResReq"

*Column 1
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table4, excel append label ctitle(`t') drop ($risk_controls $bank_controls ) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, Firm Controls, "-",Sector*County FE, "-") nocons

*Column 2-5
local bank_controls c.capital 1.foreign 1.small 1.gov 

foreach k of local bank_controls {

reghdfe `d' ResReq $risk_controls $bank_controls c.ResReq#`k' if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table4, excel append label ctitle(`t') drop ($risk_controls $bank_controls ) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, Firm Controls, "-",Sector*County FE, "-") nocons

}

*Column 6

global bank_controls_all   c.ResReq#c.capital  c.ResReq#1.foreign c.ResReq#1.small c.ResReq#1.gov

reghdfe `d' ResReq $risk_controls $bank_controls $bank_controls_all if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table4, excel append label ctitle(`t') drop ($risk_controls $bank_controls ) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, Firm Controls, "-",Sector*County FE, "-") nocons
/*
reghdfe `d' RR $risk_controls $bank_controls  $bank_controls_all $firm_controls  if !mi(`t') , absorb(firm_industry2#municipality) vce(cluster $clusters)
outreg2 using Table4, excel append label ctitle(`t') drop ($risk_controls $bank_controls $firm_controls) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, No, Firm Controls, Yes,Sector*County FE, Yes) nocons
*/
drop ResReq
}
}

/**********************
  * Creating Table 5*
 **********************/ 
 

local dep_var $dep_var
local time_var tightening

foreach d of local dep_var {
foreach t of local time_var {


gen ResReq = wRRtightening

label var ResReq "ResReq"

*Column 1
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table5, excel append label ctitle(`t') drop ($risk_controls $bank_controls ) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, Firm Controls, "-",Sector*County FE, "-") nocons

*Column 2-5
local bank_controls c.capital 1.foreign 1.small 1.gov 

foreach k of local bank_controls {

reghdfe `d' ResReq $risk_controls $bank_controls c.ResReq#`k' if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table5, excel append label ctitle(`t') drop ($risk_controls $bank_controls ) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, Firm Controls, "-",Sector*County FE, "-") nocons

}

*Column 6

global bank_controls_all   c.ResReq#c.capital  c.ResReq#1.foreign c.ResReq#1.small c.ResReq#1.gov

reghdfe `d' ResReq $risk_controls $bank_controls $bank_controls_all if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table5, excel append label ctitle(`t') drop ($risk_controls $bank_controls ) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, Firm Controls, "-",Sector*County FE, "-") nocons

/*
reghdfe `d' RR $risk_controls $bank_controls  $bank_controls_all $firm_controls  if !mi(`t') , absorb(firm_industry2#municipality) vce(cluster $clusters)
outreg2 using Table5, excel append label ctitle(`t') drop ($risk_controls $bank_controls $firm_controls) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, No, Firm Controls, Yes,Sector*County FE, Yes) nocons
*/
drop ResReq
}
}

* Bring the comprehensive sample to complete Tables 4 and 5
preserve 
	tempfile one
	* This file contains Pseudo Data for the two RR episodes (easing and tightening)
	use "$dta_file/PseudoData_RR.dta", clear


	** Eliminate defaulted loans
	drop if L3_firm_default==1

	* Defining loan-level dependent
	local dep_var wd3ln_amount_bank


	* Building max_bank
	gen abank= exp(ln_L3_amount_bank) 
	bysort firm_id time: egen afirm2 = sum(abank)
	gen afirm= exp(ln_L3_amount_firm) 
	gen mshare2= abank/afirm
	bysort firm_id time: egen mx_bank=max(mshare2)
	gen mmx_bank_id=bank_id if mx_bank == mshare2
	bysort firm_id time: egen max_bank=max(mmx_bank)
	drop mmx_bank mx_bank abank afirm*
	drop mshare2 

	* Winsorizing
	winsor2 dln3_amount_bank L3_capital L3_liquidity L3_ln_size L3_npl L3_scline L3_ln_r_maturity L3_total_interest ln_L3_amount_firm L3_ln_employees RReasing RRtightening, c(1 99)

	ren (dln3_amount_bank_w L3_capital_w L3_liquidity_w L3_ln_size_w L3_npl_w L3_scline_w L3_ln_r_maturity_w L3_total_interest_w ln_L3_amount_firm_w L3_ln_employees_w RReasing_w RRtightening_w) (wd3ln_amount_bank wL3_capital wL3_liquidity wL3_ln_size wL3_npl wL3_scline wL3_ln_r_maturity wL3_total_interest wln_L3_amount_firm wL3_ln_employees wRReasing wRRtightening)


* Set-up sample so that the counting of cases is constant at firm and loan-level 
foreach d of local dep_var {


reghdfe `d'   $risk_controls  c.wRReasing#c.wL3_total_interest if time=="easing", absorb(max_bank#bank_id max_bank#sec2mun) res(easing)

reghdfe `d'   $risk_controls  c.wRRtightening#c.wL3_total_interest  if time=="tightening", absorb(max_bank#bank_id max_bank#sec2mun) res(tightening)


keep if !mi(easing)|!mi(tightening)
drop easing tightening

}
	save `one'
	
      tempfile g

		bysort firm_id time: gen first = _n==1
		keep if first==1
		drop first
		

	foreach d of local dep_var {

	reghdfe `d'   $risk_controls  c.wRReasing  if time=="easing", absorb(max_bank sec2mun) res(easing)
	reghdfe `d'   $risk_controls  c.wRRtightening  if time=="tightening", absorb(max_bank sec2mun) res(tightening)

	keep if !mi(easing)|!mi(tightening)


		}
		 save `g', replace
		 
		 use `one'
		 
	merge m:1 firm_id time using `g', keepusing(easing tightening) keep(match) nogen
	* Sample Ready



		bysort time: center wL3_capital	
		gen capital = c_wL3_capital
		label var capital "capital"
		
		*********************
		* Last Column Table 4
		**********************
		local time_var easing 


		foreach d of local dep_var {
		foreach t of local time_var {


		gen ResReq = wRReasing
		label var ResReq "ResReq"

		*Column 7

		global bank_controls_all   c.ResReq#c.capital  c.ResReq#1.foreign c.ResReq#1.small c.ResReq#1.gov

		reghdfe `d' ResReq $risk_controls $bank_controls  $bank_controls_all $firm_controls  if !mi(`t') , absorb(sec2mun) vce(cluster $clusters)
		outreg2 using Table4, excel append label ctitle(`t') drop ($risk_controls $bank_controls $firm_controls) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, No, Firm Controls, Yes,Sector*County FE, Yes) nocons

		drop ResReq
		}
		}

		*********************
		* Last Column Table 5
		*********************
		local time_var tightening 


		foreach d of local dep_var {
		foreach t of local time_var {


		gen ResReq = wRRtightening
		label var ResReq "ResReq"

		*Column 7

		global bank_controls_all   c.ResReq#c.capital  c.ResReq#1.foreign c.ResReq#1.small c.ResReq#1.gov

		reghdfe `d' ResReq $risk_controls $bank_controls  $bank_controls_all $firm_controls  if !mi(`t') , absorb(sec2mun) vce(cluster $clusters)
		outreg2 using Table5, excel append label ctitle(`t') drop ($risk_controls $bank_controls $firm_controls) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, No, Firm Controls, Yes,Sector*County FE, Yes) nocons

		drop ResReq
		}
		}


restore 



/**********************
  * Creating Table 6*
 **********************/ 
 
 gen n_firms=0
	
local dep_var $dep_var 
local time_var  easing tightening 
local treat_var $treat_var 


foreach d of local dep_var {
foreach t of local time_var {
	
gen ResReq = cond("`t'" == "easing", wRReasing, wRRtightening) 
label var ResReq "ResReq"
		
* Counting the number of firms in each regression
unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)


* Column 1A,B and 6A,B
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6A, excel append label ctitle(`t' Full)  dec(3) nocons keep(ResReq)

reghdfe `d' ResReq $risk_controls $bank_controls $bank_controls_all if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6B, excel append label ctitle(`t' Full)  dec(3) addtext(N firms, `n_firms') nocons keep(ResReq $bank_controls_all )

* Column 2A,B and 7A,B
preserve 
keep if riskP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

center capital, i
label var capital "capital"

* Column 2A,B and 2A,B
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6A, excel append label ctitle(`t' LowRisk)  dec(3) nocons keep(ResReq)

reghdfe `d' ResReq $risk_controls $bank_controls $bank_controls_all if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6B, excel append label ctitle(`t' LowRisk)  dec(3) addtext(N firms, `n_firms') nocons keep(ResReq $bank_controls_all )

restore 

* Column 3A,B and 8A,B
preserve 
keep if riskP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

center capital, i
label var capital "capital"

* Column 2A,B and 2A,B
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6A, excel append label ctitle(`t' HighRisk)  dec(3) nocons keep(ResReq)

reghdfe `d' ResReq $risk_controls $bank_controls $bank_controls_all if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6B, excel append label ctitle(`t' HighRisk)  dec(3) addtext(N firms, `n_firms') nocons keep(ResReq $bank_controls_all )

restore 

* Column 4A,B and 9A,B
preserve 
keep if empP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

center capital, i
label var capital "capital"

* Column 2A,B and 2A,B
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6A, excel append label ctitle(`t' Large)  dec(3) nocons keep(ResReq)

reghdfe `d' ResReq $risk_controls $bank_controls $bank_controls_all if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6B, excel append label ctitle(`t' Large)  dec(3) addtext(N firms, `n_firms') nocons keep(ResReq $bank_controls_all )

restore 

* Column 5A,B and 10A,B
preserve 
keep if empP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

center capital, i
label var capital "capital"

* Column 2A,B and 2A,B
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6A, excel append label ctitle(`t' Small)  dec(3) nocons keep(ResReq)

reghdfe `d' ResReq $risk_controls $bank_controls $bank_controls_all if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table6B, excel append label ctitle(`t' Small)  dec(3) addtext(N firms, `n_firms') nocons keep(ResReq $bank_controls_all )

restore 

drop ResReq
}
}


		                     /**********************
                             * Creating Table 7 *
					        **********************/ 			 

*Prepare firm FEs to use on firm-level regressions, i.e. the "credit demand proxy" for easing and tightening 
local dep_var $dep_var  
local time_var  easing tightening

foreach d of local dep_var  {
foreach t of local time_var {

reghdfe wd3ln_amount_bank wRR`t' $risk_controls $bank_controls if !mi(`t') , absorb(f`t'=firm_id )

}
} 


				/*************************************
				  *Firm Level Regressions - Table 7
			    **************************************/

				preserve 
			
				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
		

				local r $risk_controls
		
				local risk_controls ""

				foreach v of local r  {
					
                * Weighting loan-level vars into firm-level
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
		
				local bank_controls  ""

				foreach v of global bank_controls  {
				    
				* Weighting bank-level vars into firm-level
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}

				 *Weighting the treatment variable (as in the Summary) into firm-level
				foreach v of var wRReasing wRRtightening  {
				gen e_`v' = mshare2*`v'
				}
	
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				
				* max_bank is the bank against which the firm has the largest exposure
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2
			
				
				collapse (sum) `risk_controls'  ///
								  e_wRReasing e_wRRtightening  ///	
							   `bank_controls'  ///
						 (max)  easing tightening riskP50 empP50 ///
						 $firm_controls firm_industry2 municipality wd3ln_amount_firm  max_bank* ///
						 						   feasing ftightening, by (firm_id time sec2_mun )                        
				   
				 keep if !mi(easing)|!mi(tightening)
				 

				local clusters max_bank firm_industry2 
				
			
		         * The dependent variable of Table 7
				label var wd3ln_amount_firm "d_ln_firm_credit*(100)"
				local dep_var wd3ln_amount_firm
				
				* The interactions
				label var e_gov "gov"
				label var e_foreign "foreign"
				
				center e_capital, i
				label var e_capital "capital"

				local treat_var easing tightening
				
								
				foreach d of local dep_var {
				foreach t of local treat_var {
					
				* set-up sample
				gen ResReq = e_wRR`t'
				
				reghdfe `d' ResReq  $firm_controls `bank_controls' if !mi(`t') , absorb(sec2_mun) res(re_`t')

				unique firm_industry2 if !mi(re_`t')
				gen n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				gen n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)

				gen f = f`t'
				label var f "credit demand proxy"
				local bank_int c.ResReq#c.e_capital c.ResReq#c.e_foreign c.ResReq#c.e_small c.ResReq#c.e_gov
				
				
			    foreach dep of var  wd3ln_amount_firm {

		
		        * col 1 and 4
				reghdfe `dep' ResReq `risk_controls' $firm_controls  `bank_controls' if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
				outreg2 using Table7, excel append label ctitle(`t' Dln_credit)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes, ///
				N sectors, `n_sectors', N counties, `n_counties')  nocons 
				* col 2 and 5
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table7, excel append label ctitle(`t' Dln_credit)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes, ///
				N sectors, `n_sectors', N counties, `n_counties')  nocons 
				
				* col 3 and 6
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls' `bank_int'  if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table7, excel append label ctitle(`t' Dln_credit)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes, ///
				N sectors, `n_sectors', N counties, `n_counties')  nocons 
		
								
				}
	

				drop n_sectors n_counties
				drop f
				drop ResReq
				}
				}


	restore
				
* End of Table 7




				/*************************************
				  *Firm Level Regressions - Table 8 (Split Sample)
			    **************************************/

				* Easing
				preserve 
			
				tempfile full
				
				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
		

				local r $risk_controls
		
				local risk_controls ""

				foreach v of local r  {
					
                * Weighting loan-level vars into firm-level
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
		
				local bank_controls  ""

				foreach v of global bank_controls  {
				    
				* Weighting bank-level vars into firm-level
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}

				 *Weighting the treatment variable (as in the Summary) into firm-level
				foreach v of var wRReasing wRRtightening  {
				gen e_`v' = mshare2*`v'
				}
	
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				
				* max_bank is the bank against which the firm has the largest exposure
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2
			
				
				collapse (sum) `risk_controls'  ///
								  e_wRReasing e_wRRtightening  ///	
							   `bank_controls'  ///
						 (max)  easing tightening  ///
						 $firm_controls firm_industry2 municipality wd3ln_amount_firm  max_bank* ///
						 						   feasing ftightening, by (firm_id time sec2_mun riskP50 empP50 )                        
				   
				 keep if !mi(easing)|!mi(tightening)
				 

				local clusters max_bank firm_industry2 
				
			
		         * The dependent variable of Table 7
				label var wd3ln_amount_firm "d_ln_firm_credit*(100)"
				local dep_var wd3ln_amount_firm
				
				
				local treat_var easing 
				
								
				foreach d of local dep_var {
				foreach t of local treat_var {
					
				* set-up sample
				gen ResReq = e_wRR`t'
				
				reghdfe `d' ResReq  $firm_controls `bank_controls' if !mi(`t') , absorb(sec2_mun) res(re_`t')

				unique firm_industry2 if !mi(re_`t')
				gen n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				gen n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)

				gen f = f`t'
				label var f "credit demand proxy"
			
				
				
			    foreach dep of var  wd3ln_amount_firm {

		
				* col 1
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Full)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 
				 
				 save `full'
				 
				 tempfile lowrisk
				 keep if riskP50==0&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				 * col 2
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Low-risk)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 
				 
				 			 
				 save `lowrisk'
				 
				 use `full'
				 
				 tempfile highrisk
				 
				 keep if riskP50==1&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				* col 3
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(High-risk)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 	 
				 
				 				 
				 save `highrisk'
				 
				 use `full'
				
				 tempfile large 
				 
				 keep if empP50==0&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				* col 4
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Large)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 	 
				 
				 				 
				 save `large'
				 
				 use `full'
				 
				 tempfile small
				 
				 keep if empP50==1&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				* col 4
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Small)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 	 

			    save `small'
				 
				 use `full'
								
				}
	

				drop n_sectors n_counties
				drop f
				drop ResReq
				}
				}


	restore
				
* Tightening 

 
 preserve 
			
				tempfile full
				
				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
		

				local r $risk_controls
		
				local risk_controls ""

				foreach v of local r  {
					
                * Weighting loan-level vars into firm-level
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
		
				local bank_controls  ""

				foreach v of global bank_controls  {
				    
				* Weighting bank-level vars into firm-level
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}

				 *Weighting the treatment variable (as in the Summary) into firm-level
				foreach v of var wRReasing wRRtightening  {
				gen e_`v' = mshare2*`v'
				}
	
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				
				* max_bank is the bank against which the firm has the largest exposure
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2
			
				
				collapse (sum) `risk_controls'  ///
								  e_wRReasing e_wRRtightening  ///	
							   `bank_controls'  ///
						 (max)  easing tightening  ///
						 $firm_controls firm_industry2 municipality wd3ln_amount_firm  max_bank* ///
						 						   feasing ftightening, by (firm_id time sec2_mun riskP50 empP50 )                        
				   
				 keep if !mi(easing)|!mi(tightening)
				 

				local clusters max_bank firm_industry2 
				
			
		         * The dependent variable of Table 7
				label var wd3ln_amount_firm "d_ln_firm_credit*(100)"
				local dep_var wd3ln_amount_firm
				
				
				local treat_var tightening 
				
								
				foreach d of local dep_var {
				foreach t of local treat_var {
					
				* set-up sample
				gen ResReq = e_wRR`t'
				
				reghdfe `d' ResReq  $firm_controls `bank_controls' if !mi(`t') , absorb(sec2_mun) res(re_`t')

				unique firm_industry2 if !mi(re_`t')
				gen n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				gen n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)

				gen f = f`t'
				label var f "credit demand proxy"
			
				
				
			    foreach dep of var  wd3ln_amount_firm {

		
				* col 1
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Full)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 
				 
				 save `full'
				 
				 tempfile lowrisk
				 keep if riskP50==0&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				 * col 2
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Low-risk)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 
				 
				 			 
				 save `lowrisk'
				 
				 use `full'
				 
				 tempfile highrisk
				 
				 keep if riskP50==1&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				* col 3
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(High-risk)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 	 
				 
				 				 
				 save `highrisk'
				 
				 use `full'
				
				 tempfile large 
				 
				 keep if empP50==0&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				* col 4
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Large)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 	 
				 
				 				 
				 save `large'
				 
				 use `full'
				 
				 tempfile small
				 
				 keep if empP50==1&!mi(re_`t')
				 
				unique firm_industry2 if !mi(re_`t')
				replace n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				replace n_counties = r(sum)

				
				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)
				 
				* col 4
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table8, excel append label ctitle(Small)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N sectors, `n_sectors', N counties, `n_counties', Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes)  nocons 	 

			    save `small'
				 
				 use `full'
								
				}
	

				drop n_sectors n_counties
				drop f
				drop ResReq
				}
				}


	restore
    *End of Table 8	
	
	/*******************************************************************************************************
	* Tables 9 to 11, 12A and 13A are built in the first code.
	From this point on, we build the Panel B of Tables 12 and 13. In these two tables, we put the two easing and tightening policies in comparative perspective. Tables 12A and 13A should be in your output file already.They explore easing and tightening of interest-rate policy. In contrast, we build easing and tightening of Reserve Requirements to populate Tables 12B and 13B. This is still Pseudo data. 
	*/
	 
	/* Table 12B */ 
drop n_firms	

 
gen n_firms=0
	
local dep_var $dep_var 
local time_var  easing 
local treat_var $treat_var 


foreach d of local dep_var {
foreach t of local time_var {
	

sum wRReasing if !mi(`t')
gen ResReq = wRReasing/r(sd) if !mi(`t')

label var ResReq "ResReq"
		
* Counting the number of firms in each regression
unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)


* Column (7)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12B, excel append label ctitle(Full)  dec(3) nocons addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-") keep(ResReq)


* Low-risk firms
preserve 
keep if riskP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)


* Column (8)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12B, excel append label ctitle(LowRisk)  dec(3) nocons addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  keep(ResReq)



restore 

* High-risk firms
preserve 
keep if riskP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column (9)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12B, excel append label ctitle(HighRisk)  dec(3) addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  nocons keep(ResReq)

restore 

* Large
preserve 
keep if empP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column (10)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12B, excel append label ctitle(Large)  dec(3) addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  nocons keep(ResReq)

restore 

* Small
preserve 
keep if empP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column (11)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12B, excel append label ctitle(Small)  dec(3) addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  nocons keep(ResReq)

restore 

drop ResReq
}
}
	
	
*Firm-level
				preserve 
			
				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
		

				local r $risk_controls
		
				local risk_controls ""

				foreach v of local r  {
					
                * Weighting loan-level vars into firm-level
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
		
				local bank_controls  ""

				foreach v of global bank_controls  {
				    
				* Weighting bank-level vars into firm-level
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}

				 *Weighting the treatment variable (as in the Summary) into firm-level
				foreach v of var wRReasing wRRtightening  {
				gen e_`v' = mshare2*`v'
				}
	
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				
				* max_bank is the bank against which the firm has the largest exposure
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2
			
				
				collapse (sum) `risk_controls'  ///
								  e_wRReasing e_wRRtightening  ///	
							   `bank_controls'  ///
						 (max)  easing tightening riskP50 empP50 ///
						 $firm_controls firm_industry2 municipality wd3ln_amount_firm  max_bank* ///
						 						   feasing ftightening, by (firm_id time sec2_mun )                        
				   
				 keep if !mi(easing)|!mi(tightening)
				 

				local clusters max_bank firm_industry2 
				
			
		         * The dependent variable of Table 7
				label var wd3ln_amount_firm "d_ln_firm_credit*(100)"
				local dep_var wd3ln_amount_firm
				
				local treat_var easing 
				
								
				foreach d of local dep_var {
				foreach t of local treat_var {
					
				* set-up sample
			
				
				reghdfe `d' e_wRR`t'  $firm_controls `bank_controls' if !mi(`t') , absorb(sec2_mun) res(re_`t')

				unique firm_id if !mi(re_`t')
				gen n_firms = r(sum)
					
				sum n_firms if !mi(re_`t')
				local n_firms= r(mean)

	
				gen f = f`t' if !mi(re_`t')
				label var f "credit demand proxy"
				
				sum e_wRR`t' if !mi(re_`t')
				
				gen ResReq = e_wRR`t'/r(sd) if !mi(re_`t')
				
			    foreach dep of var  wd3ln_amount_firm {

		
				* Column (12)
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table12B, excel append label ctitle(Firm-level)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N Firm, `n_firms', Firm FE, No, Firm Controls, Yes, Sector*County FE, Yes)  nocons 
				
							
				}
	

				drop n_firms
				drop f
				drop ResReq
				}
				}

	restore
					
 
	/* Table 13B */ 
drop n_firms	

 
gen n_firms=0
	
local dep_var $dep_var 
local time_var  tightening
local treat_var $treat_var 


foreach d of local dep_var {
foreach t of local time_var {
	

sum wRReasing if !mi(`t')
gen ResReq = wRReasing/r(sd) if !mi(`t')

label var ResReq "ResReq"
		
* Counting the number of firms in each regression
unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)


* Column (7)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13B, excel append label ctitle(Full)  dec(3) nocons addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-") keep(ResReq)


* Low-risk firms
preserve 
keep if riskP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)


* Column (8)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13B, excel append label ctitle(LowRisk)  dec(3) nocons addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  keep(ResReq)



restore 

* High-risk firms
preserve 
keep if riskP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column (9)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13B, excel append label ctitle(HighRisk)  dec(3) addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  nocons keep(ResReq)

restore 

* Large
preserve 
keep if empP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column (10)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13B, excel append label ctitle(Large)  dec(3) addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  nocons keep(ResReq)

restore 

* Small
preserve 
keep if empP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column (11)
reghdfe `d' ResReq $risk_controls $bank_controls if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13B, excel append label ctitle(Small)  dec(3) addtext(N Firm, `n_firms', Firm FE, Yes, Firm Controls, "-", Sector*County FE, "-")  nocons keep(ResReq)

restore 

drop ResReq
}
}
	
	
*Firm-level
				preserve 
			
				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
		

				local r $risk_controls
		
				local risk_controls ""

				foreach v of local r  {
					
                * Weighting loan-level vars into firm-level
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
		
				local bank_controls  ""

				foreach v of global bank_controls  {
				    
				* Weighting bank-level vars into firm-level
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}

				 *Weighting the treatment variable (as in the Summary) into firm-level
				foreach v of var wRReasing wRRtightening  {
				gen e_`v' = mshare2*`v'
				}
	
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				
				* max_bank is the bank against which the firm has the largest exposure
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2
			
				
				collapse (sum) `risk_controls'  ///
								  e_wRReasing e_wRRtightening  ///	
							   `bank_controls'  ///
						 (max)  easing tightening riskP50 empP50 ///
						 $firm_controls firm_industry2 municipality wd3ln_amount_firm  max_bank* ///
						 						   feasing ftightening, by (firm_id time sec2_mun )                        
				   
				 keep if !mi(easing)|!mi(tightening)
				 

				local clusters max_bank firm_industry2 
				
			
		         * The dependent variable of Table 7
				label var wd3ln_amount_firm "d_ln_firm_credit*(100)"
				local dep_var wd3ln_amount_firm
				
				local treat_var tightening
				
								
				foreach d of local dep_var {
				foreach t of local treat_var {
					
				* set-up sample
			
				
				reghdfe `d' e_wRR`t'  $firm_controls `bank_controls' if !mi(`t') , absorb(sec2_mun) res(re_`t')

				unique firm_id if !mi(re_`t')
				gen n_firms = r(sum)
					
				sum n_firms if !mi(re_`t')
				local n_firms= r(mean)

	
				gen f = f`t' if !mi(re_`t')
				label var f "credit demand proxy"
				
				sum e_wRR`t' if !mi(re_`t')
				
				gen ResReq = e_wRR`t'/r(sd) if !mi(re_`t')
				
			    foreach dep of var  wd3ln_amount_firm {

		
				* Column (12)
				reghdfe `dep' ResReq f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(sec2_mun) vce(cluster `clusters')
					outreg2 using Table13B, excel append label ctitle(Firm-level)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(N Firm, `n_firms', Firm FE, No, Firm Controls, Yes, Sector*County FE, Yes)  nocons 
				
							
				}
	

				drop n_firms
				drop f
				drop ResReq
				}
				}

	restore
 * End of Table 13B
 
 
 /** End of do-file **/
 
 /** End of all replication codes **/
 
 
 
